name: Scada-LTS PR development workflow
on:
  pull_request:
    types: [opened, synchronize, reopened]
jobs:

  compile:
    name: Compile and Test Scada-LTS application
    runs-on: ubuntu-latest
    env:
      CATALINA_HOME: /home/runner/tomcat/apache-tomcat-9.0.86
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Set up JDK 11
        uses: actions/setup-java@v2
        with:
          java-version: '11'
          distribution: 'adopt'
      - name: Cache Tomcat
        uses: actions/cache@v3
        with:
          key: ${{ runner.os }}-tomcat-${{ github.run_id }}
          path: /home/runner/tomcat
          restore-keys: |
            ${{ runner.os }}-tomcat-
      - name: Install Tomcat
        run: mkdir -p /home/runner/tomcat; cd /home/runner/tomcat; wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.86/bin/apache-tomcat-9.0.86.tar.gz; tar xvzf apache-tomcat-9.0.86.tar.gz
      - name: Show Tomcat
        run: ls $CATALINA_HOME
      - name: Test JUnit Scada Application
        run: gradle test
      - name: Show JUnit Test Results
        uses: EnricoMi/publish-unit-test-result-action@v1
        if: always()
        with:
          check_name: Java JUnit Test Results
          files: ./build/test-results/**/*.xml
      - name: Publish JUnit Test Results
        uses: actions/upload-artifact@v2
        with:
          name: junit-java-test-results
          path: ./build/test-reports/
      - name: Build Scada-LTS WAR Application
        run: gradle -PskipUi=true compileJava
      - name: Cache Scada-LTS application
        uses: actions/cache@v3
        with:
          key: ${{ runner.os }}-slts-${{ github.run_id }}
          path: ./build/

  buildui:
    name: Test and Build Scada-LTS new User Interface
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 14.x
      - name: Cache node modules
        id: nodeCache
        uses: actions/cache@v3
        with:
          path: ./scadalts-ui/node_modules
          key: ${{ runner.os }}-node-${{ github.run_id }}-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-${{ github.run_id }}-${{ hashFiles('**/package-lock.json') }}
      - name: Install dependencies
        if: steps.nodeCache.outputs.cache-hit != 'true'
        working-directory: ./scadalts-ui
        run: npm install
      - name: Run Frontend UnitTests
        working-directory: ./scadalts-ui
        run: npm run-script test:unit
      - name: Show Mocha Test Results
        uses: EnricoMi/publish-unit-test-result-action@v1
        if: always()
        with:
          check_name: Java Script Mocha Unit Test Results
          files: ./scadalts-ui/test-results.xml
      - name: Publish Mocha Test Results
        uses: actions/upload-artifact@v2
        with:
          name: mocha-js-test-results
          path: ./scadalts-ui/test-results.xml
      - name: Get Scada-LTS latest release version
        id: relver
        uses: pozetroninc/github-action-get-latest-release@master
        with:
          owner: SCADA-LTS
          repo: Scada-LTS
      - name: Prepare Application Vairables
        working-directory: ./scadalts-ui
        run: npm run-script build-config -- ${{ steps.relver.outputs.release }} $GITHUB_RUN_ID $GITHUB_HEAD_REF $GITHUB_SHA $GITHUB_ACTOR $GITHUB_BASE_REF
      - name: Build User Interface
        working-directory: ./scadalts-ui
        run: npm run-script build
      - name: Cache Frontend
        uses: actions/cache@v3
        with:
          key: ${{ runner.os }}-frontend-${{ github.run_id }}
          path: ./scadalts-ui/dist

  war:
    name: Build WAR archive
    needs: [compile, buildui]
    runs-on: ubuntu-latest
    env:
      CATALINA_HOME: /home/runner/tomcat/apache-tomcat-9.0.86
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Set up JDK
        uses: actions/setup-java@v2
        with:
          java-version: 11
          distribution: 'adopt'
      - name: Load Cached node modules
        uses: actions/cache@v3
        with:
          path: ./scadalts-ui/node_modules
          key: ${{ runner.os }}-node-${{ github.run_id }}-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-${{ github.run_id }}-${{ hashFiles('**/package-lock.json') }}
      - name: Load Cached Scada-LTS application
        uses: actions/cache@v3
        with:
          key: ${{ runner.os }}-slts-${{ github.run_id }}
          path: ./build/
      - name: Load Cached Frontend
        uses: actions/cache@v3
        with:
          key: ${{ runner.os }}-frontend-${{ github.run_id }}
          path: ./scadalts-ui/dist
      - name: Get Scada-LTS latest release version
        id: relverjava
        uses: pozetroninc/github-action-get-latest-release@master
        with:
          owner: SCADA-LTS
          repo: Scada-LTS
      - name: Prepare Application Enviroment Version Vairables
        working-directory: ./
        run: ./version.sh ${{ steps.relverjava.outputs.release }} $GITHUB_RUN_ID $GITHUB_HEAD_REF $GITHUB_SHA $GITHUB_ACTOR $GITHUB_BASE_REF
      - name: Install static web-dependencies
        working-directory: ./WebContent/resources
        run: npm install
      - name: Build Scada Application
        run: gradle -PskipUi=true war
      - name: Save WAR file to cache
        uses: actions/cache@v3
        with:
          key: ${{ runner.os }}-war-${{ github.run_id }}
          path: ./build/libs/Scada-LTS.war

  deploy-artefact:
    name: Deploy Scada-LTS as Artifact
    needs: [war]
    runs-on: ubuntu-latest
    steps:
      - name: Load WAR file to cache
        uses: actions/cache@v3
        with:
          key: ${{ runner.os }}-war-${{ github.run_id }}
          path: ./build/libs/Scada-LTS.war
      - name: Deploy WAR artifact
        uses: actions/upload-artifact@v2
        with:
          name: Scada-LTS
          path: ./build/libs/Scada-LTS.war

  deploy-docker:
    name: Deploy Scada-LTS as Docker Image
    needs: [ war ]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Load WAR file to cache
        uses: actions/cache@v3
        with:
          key: ${{ runner.os }}-war-${{ github.run_id }}
          path: ./build/libs/Scada-LTS.war
      - name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v3
        with:
          images: scadalts/scadalts
      - name: Build and push Docker image
        uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
      - name: Build and push nightly Docker image
        if: ${{ startsWith(github.base_ref, 'release') || startsWith(github.base_ref, 'preview') }}
        uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          tags: scadalts/scadalts:nightly
          labels: ${{ steps.meta.outputs.labels }}
